package cn.wit.summit.chwan.widgets;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import cn.wit.summit.chwan.interfaces.ScrollHeightListener;
import cn.wit.summit.chwan.utils.LogUtils;

/**
 * Created by Alan on 2018/9/19.
 */

public class CustomLinearLayout extends LinearLayout {

    private ScrollHeightListener mScrollHeightListener;
    private int oldStatY;
    private int downY;

    public CustomLinearLayout(Context context) {
        super(context);
    }

    public CustomLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    private boolean mScrolling;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) getLayoutParams();
        LogUtils.e("tbl>>>layoutParams.topMargin:"+layoutParams.topMargin);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = (int) event.getRawX();
//                oldStatY = layoutParams.topMargin;
                mScrolling = false;
                break;
            case MotionEvent.ACTION_MOVE:
//                int offsetY = (int)event.getRawX() - downY;
//                int top = offsetY + oldStatY;
                if (Math.abs(downY - event.getRawX()) >= ViewConfiguration.get(getContext()).getScaledTouchSlop()) {
                    mScrolling = true;
//                    getParent().requestDisallowInterceptTouchEvent(true);
                } else {
                    mScrolling = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                mScrolling = false;
                break;
        }
        return mScrolling;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    //    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                break;
//            case MotionEvent.ACTION_MOVE:
//                int offsetY = (int)event.getRawY() - downY;
//                int top = offsetY + oldStatY;
//                LogUtils.e("tbl>>>top:"+top);
//                if (top>=0) {
//                    layoutParams.topMargin = 0;
//                    setLayoutParams(layoutParams);
////                    return false;
//                } else {
//                    layoutParams.topMargin = top;
//                    setLayoutParams(layoutParams);
////                    if (mScrollHeightListener!=null)
////                        mScrollHeightListener.OnTopScroll(top);
//                }
//                break;
//            case MotionEvent.ACTION_UP:
//                break;
//        }
//        return true;
//    }

    public void setmScrollHeightListener(ScrollHeightListener mScrollHeightListener) {
        this.mScrollHeightListener = mScrollHeightListener;
    }
}
